{% include 'header.html' %}

  <title>Wox Status: {{dj_title}}</title>
  {% load socketio_tags %}
  {% socketio %}
  <script type="text/javascript"> 

    // global variables for status tracking

    // test status
    var tstatus = {};
    var firstsync_flag = false;

    function getlen(jsonobj) {
        var l = 0;
        for (i in jsonobj)
            l++;
        return l;
    }

    function magic() {
        // will be instanced in start()
        var socket;
    
        var connected = function() {
            // use log id as channel name
            for (c in tstatus) {
                socket.subscribe(c);
            }

            // send sync request to server
            // do it just once at beginning
            if (firstsync_flag && getlen(tstatus)) {
                firstsync_flag = false;
                socket.send({channels: tstatus});
            }
        };
    
        var disconnected = function() {
            // reconnect after 1s
            setTimeout(start, 1000);
        };
    
        var messaged = function(data_array) {
          $.each(data_array, function(i, data) {
            var c = data.channel;
            if (!(c in tstatus)) {
                // continue
                return;
            }

            var s = data.status;
            if (tstatus[c] != s) {
                tstatus[c] = s;
                switch (s) {
                    case 'error':
                    case 'terminated':
                    case 'timeout':
                    case 'cancelled':
                    case 'finished':
                        socket.unsubscribe(c);
                        delete tstatus[c];
                        break;
                }

                // resetting title bar class value to change the color
                var woxs_bar = $('#' + c);
                woxs_bar.attr('class', 'woxst-sum-bar loading-indicator-center');
                setTimeout(function() { 
                    woxs_bar.attr('class', 'woxst-sum-bar woxst_' + s);
                    woxs_bar.html(s);
                }, 500);
            }
          }); // end of each

        }; // end of messaged
    
        var start = function() {
            socket = new io.Socket();
            socket.connect();
            socket.on('connect', connected);
            socket.on('disconnect', disconnected);
            socket.on('message', messaged);
        };
    
        start();
    } // end of magic()

    $(document).ready(function() {

{% include 'topnav-imd.js' %}

        // initialize global variables 
        $('.woxst-sum-bar').each(function() {
            var id = $(this).attr('id');
            var s = $('#' + id).html();
            // check both install and test status here
            switch (s) {
                case 'error':
                case 'terminated':
                case 'timeout':
                case 'cancelled':
                case 'finished':
                    break;
                case 'scheduled':
                case 'started':
                    firstsync_flag = true;
                    tstatus[id] = s;
                    break;
            }

        }); // end of each

        magic();
    }); // end of ready()

  </script>

</head>

<body class="defect-opensolaris-org-bz">
{% include 'topnav.html' %}
<div>
  <table id="tabtitles" border="0" cellpadding="0" cellspacing="0">
  <tbody><tr>
    <td id="title">
    <p><font color="#FFFFFF">&nbsp;Status &raquo; {{dj_title}}</font></p>
    </td></tr>
  </tbody></table>
</div>

<div id="bugzilla-body" style="min-height: 450px;">
{% if dj_jobs %}
<table class="bz_buglist" cellpadding="4" cellspacing="0" width="100%">
<tbody>
  <tr class="bz_buglist_header" align="left">
  <th class="first-child">Machine Group</th>
  <th>Build NO.</th>
  <th>Machine Info.</th>
  <th>Testrun</th>
  <!--
  <th>More Info.</th>
  -->
  </tr>

  {% for j in dj_jobs %}

  <tr class="bz_bugitem {% cycle 'bz_row_even' 'bz_row_odd' %}">
  <td class="first-child">{{j.db.mgroup.name}}</td>
  <td>{% if j.db.test_machine.os_bldno and j.db.test_machine.dt_bldno %}
    os: {{j.db.test_machine.os_bldno}}<br> dt: {{j.db.test_machine.dt_bldno}}</td>
      {% else %}
      -
      {% endif %}
  </td>
  <td>{{j.db.test_machine.hostname}} 
    {% if j.db.hwconf %}
      ({{j.db.hwconf.hwmode.name}}/{{j.db.hwconf.hwmode.get_arch_display}})
      <br>pcpu: {{j.db.hwconf.psr_pnumber}} 
      <br>vcpu: {{j.db.hwconf.psr_vnumber}}
      <br>clock: {{j.db.hwconf.psr_ckspeed}}
      <br>mem: {{ j.db.hwconf.mem_size|cut:'egabytes' }}B
    {% endif %}
  </td>
  <td>
    <table><tr>
    <td>
      <table>
    {% for tr in j.testruns %}
      <tr>
        <td class="td-label">{{tr.name}}:</td>
        <td><div id="{{tr.id}}" class="woxst-sum-bar woxst_{{tr.status}}">{{tr.status}}</div></td>
      </tr>
    {% endfor %}
      </table>
    </td>
    <td><a href="test?tid={{j.tidurl}}">More Details</a></td>
    </tr>  
    </table>
  </td>
  </tr>
  {% endfor %}

  <tr><td class="first-child" colspan="7">
    <hr>
  </td></tr>

</tbody></table>

{% else %}
<p>No job found</p>
<p></p>
{% endif %}
</div>
<!-- end of div bugzilla-body -->


{% include 'footer.html' %}
